<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?= isset($title) ? htmlspecialchars($title) : 'MyCMS' ?></title>
    <style>
        /* 暗色主题样式 */
        :root {
            --primary-color: #61dafb;
            --secondary-color: #bb86fc;
            --background-color: #121212;
            --text-color: #e0e0e0;
            --link-color: #61dafb;
            --header-bg: #1e1e1e;
            --footer-bg: #1e1e1e;
            --card-bg: #2d2d2d;
            --border-color: #444;
        }

        * {
            box-sizing: border-box;
            margin: 0;
            padding: 0;
        }

        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            line-height: 1.6;
            color: var(--text-color);
            background-color: var(--background-color);
            padding-top: 80px;
        }

        .container {
            width: 100%;
            max-width: 1200px;
            margin: 0 auto;
            padding: 0 15px;
        }

        header {
            background-color: var(--header-bg);
            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);
            position: fixed;
            top: 0;
            left: 0;
            right: 0;
            z-index: 1000;
        }

        .header-content {
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 1rem 0;
        }

        .logo {
            font-size: 1.5rem;
            font-weight: bold;
            color: var(--primary-color);
            text-decoration: none;
        }

        nav ul {
            display: flex;
            list-style: none;
        }

        nav ul li {
            margin-left: 1.5rem;
        }

        nav ul li a {
            color: var(--text-color);
            text-decoration: none;
            font-weight: 500;
            transition: color 0.3s;
        }

        nav ul li a:hover {
            color: var(--primary-color);
        }

        main {
            padding: 2rem 0;
        }

        footer {
            background-color: var(--footer-bg);
            border-top: 1px solid var(--border-color);
            padding: 2rem 0;
            margin-top: 3rem;
        }

        .footer-content {
            display: flex;
            justify-content: space-between;
            align-items: center;
        }

        .btn {
            display: inline-block;
            background-color: var(--primary-color);
            color: #000;
            padding: 0.5rem 1rem;
            border-radius: 4px;
            text-decoration: none;
            font-weight: 500;
            border: none;
            cursor: pointer;
            transition: background-color 0.3s;
        }

        .btn:hover {
            background-color: #4db8e0;
        }

        .btn-secondary {
            background-color: var(--secondary-color);
        }

        .btn-secondary:hover {
            background-color: #a06be4;
        }

        .card {
            background-color: var(--card-bg);
            border-radius: 8px;
            box-shadow: 0 2px 8px rgba(0, 0, 0, 0.3);
            margin-bottom: 1.5rem;
            overflow: hidden;
        }

        .card-header {
            padding: 1rem 1.5rem;
            border-bottom: 1px solid var(--border-color);
            display: flex;
            justify-content: space-between;
            align-items: center;
        }

        .card-body {
            padding: 1.5rem;
        }

        .card-footer {
            padding: 1rem 1.5rem;
            border-top: 1px solid var(--border-color);
        }

        .alert {
            padding: 1rem;
            border-radius: 4px;
            margin-bottom: 1rem;
        }

        .alert-success {
            background-color: #1e4620;
            color: #8eff8e;
            border: 1px solid #2a6b2a;
        }

        .alert-danger {
            background-color: #4c1d1b;
            color: #ff8a8a;
            border: 1px solid #6b2a2a;
        }

        .alert-warning {
            background-color: #4d3800;
            color: #ffd875;
            border: 1px solid #6b5a2a;
        }

        .alert-info {
            background-color: #1a3b4a;
            color: #86cfff;
            border: 1px solid #2a526b;
        }

        .form-group {
            margin-bottom: 1rem;
        }

        .form-group label {
            display: block;
            margin-bottom: 0.5rem;
            font-weight: 500;
        }

        .form-control {
            width: 100%;
            padding: 0.75rem;
            font-size: 1rem;
            background-color: #3a3a3a;
            color: var(--text-color);
            border: 1px solid var(--border-color);
            border-radius: 4px;
        }

        .form-control:focus {
            outline: none;
            border-color: var(--primary-color);
            box-shadow: 0 0 0 0.2rem rgba(97, 218, 251, 0.25);
        }

        .table {
            width: 100%;
            border-collapse: collapse;
        }

        .table th,
        .table td {
            padding: 0.75rem;
            border-bottom: 1px solid var(--border-color);
        }

        .table th {
            text-align: left;
            background-color: #1e1e1e;
        }

        .pagination {
            display: flex;
            list-style: none;
            justify-content: center;
            margin-top: 2rem;
        }

        .pagination li {
            margin: 0 0.25rem;
        }

        .pagination a {
            display: block;
            padding: 0.5rem 0.75rem;
            border: 1px solid var(--border-color);
            border-radius: 4px;
            color: var(--text-color);
            text-decoration: none;
        }

        .pagination a:hover {
            background-color: #3a3a3a;
        }

        .pagination .active a {
            background-color: var(--primary-color);
            color: #000;
            border-color: var(--primary-color);
        }

        @media (max-width: 768px) {
            .header-content {
                flex-direction: column;
                padding: 1rem;
            }

            nav ul {
                margin-top: 1rem;
            }

            nav ul li {
                margin-left: 1rem;
                margin-right: 1rem;
            }

            .footer-content {
                flex-direction: column;
                text-align: center;
            }

            .footer-content div:last-child {
                margin-top: 1rem;
            }
        }
    </style>
</head>

<body>
    <header>
        <div class="container">
            <div class="header-content">
                <a href="/" class="logo">MyCMS - 暗色主题</a>
                <nav>
                    <ul>
                        <li><a href="/">首页</a></li>
                        <li><a href="/posts">文章</a></li>
                        <?php
                        $currentUser = null;
                        try {
                            $currentUser = App\Services\Auth::user();
                        } catch (Exception $e) {
                            // 忽略错误
                        }
                        if ($currentUser): ?>
                            <li><a href="/posts/create">写文章</a></li>
                            <?php if (isset($currentUser['is_admin']) && $currentUser['is_admin']): ?>
                                <li><a href="/admin">管理后台</a></li>
                            <?php endif; ?>
                            <li>
                                <form action="/logout" method="post" style="display: inline;">
                                    <?php echo App\Core\CSRF::field(); ?>
                                    <button type="submit" style="background: none; border: none; cursor: pointer; color: var(--text-color); font-weight: 500;">退出</button>
                                </form>
                            </li>
                        <?php else: ?>
                            <li><a href="/login">登录</a></li>
                            <li><a href="/register">注册</a></li>
                        <?php endif; ?>
                    </ul>
                </nav>
            </div>
        </div>
    </header>

    <main class="container">
        <?php if (isset($isPreview) && $isPreview): ?>
            <div class="alert alert-warning" style="display: flex; justify-content: space-between; align-items: center;">
                <div>
                    <strong>主题预览模式</strong> - 您正在预览 "<?= htmlspecialchars($currentTheme) ?>" 主题。这只对您可见，不会影响其他用户。
                </div>
                <div>
                    <a href="/theme/switch?theme=<?= urlencode($currentTheme) ?>" class="btn btn-sm" style="margin-right: 10px;">激活此主题</a>
                    <a href="/theme/preview" class="btn btn-secondary btn-sm">退出预览</a>
                </div>
            </div>
        <?php endif; ?>

        <?php
        // 显示 Flash 消息
        try {
            if (App\Core\Flash::has('success')): ?>
                <div class="alert alert-success">
                    <?= App\Core\Flash::get('success') ?>
                </div>
            <?php endif;

            if (App\Core\Flash::has('error')): ?>
                <div class="alert alert-danger">
                    <?= App\Core\Flash::get('error') ?>
                </div>
            <?php endif;

            if (App\Core\Flash::has('warning')): ?>
                <div class="alert alert-warning">
                    <?= App\Core\Flash::get('warning') ?>
                </div>
            <?php endif;

            if (App\Core\Flash::has('info')): ?>
                <div class="alert alert-info">
                    <?= App\Core\Flash::get('info') ?>
                </div>
        <?php endif;
        } catch (Exception $e) {
            // 忽略错误
        }
        ?>

        <?= $content ?>
    </main>

    <footer>
        <div class="container">
            <div class="footer-content">
                <div>
                    <p>&copy; <?= date('Y') ?> MyCMS. 保留所有权利。</p>
                </div>
                <div>
                    <p>当前主题: <?= htmlspecialchars($currentTheme ?? 'dark') ?> | <a href="/theme/switch?theme=default" style="color: var(--primary-color);">切换到默认主题</a> | <a href="/theme/switch?theme=light" style="color: var(--primary-color);">切换到亮色主题</a></p>
                </div>
            </div>
        </div>
    </footer>
</body>

</html>